package com.txzt.gaea.business.project.service.impl;

import com.txzt.gaea.business.plan.domain.model.PlanInfo;
import com.txzt.gaea.business.project.domain.model.ProjectInfo;
import com.txzt.gaea.business.project.domain.model.ProjectLog;
import com.txzt.gaea.business.project.domain.vo.ProjectInfoVo;
import com.txzt.gaea.business.project.service.IProjectInfoService;
import com.txzt.gaea.business.project.service.IProjectLogService;
import com.txzt.gaea.business.util.TranslateUtil;
import com.txzt.gaea.common.base.service.impl.BaseServiceImpl;
import com.txzt.gaea.common.core.base.context.AdminUserContext;
import com.txzt.gaea.common.enums.FlowFlagEnum;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import com.txzt.gaea.common.enums.OperateType;
import com.txzt.gaea.common.enums.ProjectStatus;
import com.txzt.gaea.flow.service.FlowService;
import com.txzt.gaea.flow.vo.FlowBizStepVo;
import com.txzt.gaea.provide.service.FileInfoService;
import com.txzt.gaea.vo.AdminUserVO;
import org.beetl.sql.core.query.LambdaQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProjectInfoServiceImpl extends BaseServiceImpl<ProjectInfo> implements IProjectInfoService {

    @Autowired
    private FlowService flowService;
    @Autowired
    private IProjectLogService projectLogService;
    @Autowired
    private FileInfoService fileInfoService;

    @Override
    public List<Map> statisticsProjectInfo() {
        return sqlManager.select("projectInfo.statisticsProjectStatus",Map.class,null);
    }

    @Override
    public Map<String,Object> getProjectNum(){
        Map<String,Object> map = new HashMap<>();
        LambdaQuery<ProjectInfo> projectQuery = sqlManager.lambdaQuery(ProjectInfo.class);
        map.put("total",projectQuery.andEq(ProjectInfo::getIsDelete, IsDeleteEnum.NOTDELETE.getCode()).count());
        projectQuery = sqlManager.lambdaQuery(ProjectInfo.class);
        map.put("noOpen",projectQuery.andEq(ProjectInfo::getIsDelete, IsDeleteEnum.NOTDELETE.getCode())
                .andEq(ProjectInfo::getProjectStatus,ProjectStatus.INIT.getCode()).count());
        projectQuery = sqlManager.lambdaQuery(ProjectInfo.class);
        map.put("open",projectQuery.andEq(ProjectInfo::getIsDelete, IsDeleteEnum.NOTDELETE.getCode())
                .andEq(ProjectInfo::getProjectStatus,ProjectStatus.OPEN.getCode()).count());
        projectQuery = sqlManager.lambdaQuery(ProjectInfo.class);
        map.put("delay",projectQuery.andEq(ProjectInfo::getIsDelete, IsDeleteEnum.NOTDELETE.getCode())
                .andEq(ProjectInfo::getProjectStatus,ProjectStatus.DELAY.getCode()).count());
        projectQuery = sqlManager.lambdaQuery(ProjectInfo.class);
        map.put("delayCheck",projectQuery.andEq(ProjectInfo::getIsDelete, IsDeleteEnum.NOTDELETE.getCode())
                .andEq(ProjectInfo::getProjectStatus,ProjectStatus.DELAY_CHECK.getCode()).count());
        projectQuery = sqlManager.lambdaQuery(ProjectInfo.class);
        map.put("finish",projectQuery.andEq(ProjectInfo::getIsDelete, IsDeleteEnum.NOTDELETE.getCode())
                .andEq(ProjectInfo::getProjectStatus,ProjectStatus.FINISH.getCode()).count());
        projectQuery = sqlManager.lambdaQuery(ProjectInfo.class);
        map.put("finishCheck",projectQuery.andEq(ProjectInfo::getIsDelete, IsDeleteEnum.NOTDELETE.getCode())
                .andEq(ProjectInfo::getProjectStatus,ProjectStatus.FINISH_CHECK.getCode()).count());
        projectQuery = sqlManager.lambdaQuery(ProjectInfo.class);
        map.put("stop",projectQuery.andEq(ProjectInfo::getIsDelete, IsDeleteEnum.NOTDELETE.getCode())
                .andEq(ProjectInfo::getProjectStatus,ProjectStatus.STOP.getCode()).count());
        projectQuery = sqlManager.lambdaQuery(ProjectInfo.class);
        map.put("stopCheck",projectQuery.andEq(ProjectInfo::getIsDelete, IsDeleteEnum.NOTDELETE.getCode())
                .andEq(ProjectInfo::getProjectStatus,ProjectStatus.STOP_CHECK.getCode()).count());
        return map;
    }

    @Override
    public ProjectInfo selectProjectInfoById(Long id) {
        ProjectInfo projectInfo = sqlManager.single(ProjectInfo.class,id);
        if (null == projectInfo) {
            return null;
        }
        //查询计划审核流程步骤
        List<FlowBizStepVo> planFlowStep = flowService.getFlowBizStep(projectInfo.getPlanId(),FlowFlagEnum.MAKING_PLAN.getCode());
        //查询项目当前审核流程步骤
        List<FlowBizStepVo> projectFlowStep = flowService.getFlowBizStep(projectInfo.getFlowId());
        projectInfo.setPlanFlowStep(planFlowStep);
        projectInfo.setProjectFlowStep(projectFlowStep);
        //翻译项目组长
        projectInfo.setProjectLeaderName(TranslateUtil.getUserName(projectInfo.getProjectLeader()));
        //翻译项目组长
        projectInfo.setDepartName(TranslateUtil.getDepartmentName(projectInfo.getDepartId()));
        //判断当前操作人是否可审核流程
        projectInfo.setCheck(TranslateUtil.getCheck(projectFlowStep));
        return projectInfo;
    }

    @Override
    public ProjectInfo selectProjectInfoByPlanId(Long planId) {
        return sqlManager.query(ProjectInfo.class).andEq("plan_id",planId).andEq("is_delete", IsDeleteEnum.NOTDELETE.getCode()).single();
    }

    @Override
    public ProjectInfo selectProjectInfoByNo(String projectNo){
        return sqlManager.query(ProjectInfo.class).andEq("project_no",projectNo).andEq("is_delete", IsDeleteEnum.NOTDELETE.getCode()).single();
    }

    @Override
    public int insertProjectInfo(ProjectInfo projectInfo) {
        return sqlManager.insert(projectInfo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int openProjectInfo(ProjectInfoVo projectInfoVo) {
        ProjectInfo ori = sqlManager.single(ProjectInfo.class,projectInfoVo.getId());
        //开启流程
        Long flowBizId = flowService.startFlow(projectInfoVo.getId(), "tb_project_info","project_status",
                ProjectStatus.INIT.getCode(),ProjectStatus.OPEN.getCode(), FlowFlagEnum.PROJECT_OPENING.getCode());
        //记录日志
        ProjectLog log = new ProjectLog();
        log.setProjectId(projectInfoVo.getId());
        log.setFlowId(flowBizId);
        log.setContentText(projectInfoVo.getGistSense());
        log.setContentTarget(projectInfoVo.getResearchTarget());
        log.setFilePath(projectInfoVo.getReportFile());
        log.setOperateType(OperateType.OPEN_CHECK.getCode());
        AdminUserVO adminUserVO = AdminUserContext.getUser();
        log.setCreateBy(adminUserVO.getId());
        log.setCreateTime(new Date());
        projectLogService.insertTemplate(log);
        //更新项目
        ProjectInfo projectInfo = new ProjectInfo();
        projectInfo.setId(projectInfoVo.getId());
        projectInfo.setFlowId(flowBizId);
        projectInfo.setProjectStatus(ProjectStatus.OPEN_CHECK.getCode());
        projectInfo.setGistSense(projectInfoVo.getGistSense());
        projectInfo.setResearchTarget(projectInfoVo.getResearchTarget());
        projectInfo.setReportFile(projectInfoVo.getReportFile());
        //文件处理
        fileInfoService.fileChange(ori.getReportFile(),projectInfoVo.getReportFile(),ori.getProjectNo(),2);
        return sqlManager.updateTemplateById(projectInfo);
    }



    @Transactional(rollbackFor = Exception.class)
    @Override
    public int delayProjectInfo(ProjectInfoVo projectInfoVo) {
        ProjectInfo res = sqlManager.single(ProjectInfo.class,projectInfoVo.getId());
        //开启流程
        Long flowBizId = flowService.startFlow(res.getId(), "tb_project_info","project_status",
                res.getProjectStatus(),ProjectStatus.DELAY.getCode(), FlowFlagEnum.PROJECT_DELAY.getCode());
        //记录日志
        ProjectLog log = new ProjectLog();
        log.setProjectId(res.getId());
        log.setFlowId(flowBizId);
        //计划完成时间
        log.setPlanTime(projectInfoVo.getPlanTime());
        //预计完成时间
        log.setActualTime(projectInfoVo.getActualTime());
        //延期完成理由
        log.setContentText(projectInfoVo.getContentText());
        log.setOperateType(OperateType.DELAY_CHECK.getCode());
        AdminUserVO adminUserVO = AdminUserContext.getUser();
        log.setCreateBy(adminUserVO.getId());
        log.setCreateTime(new Date());
        projectLogService.insertTemplate(log);
        //更新项目
        ProjectInfo projectInfo = new ProjectInfo();
        projectInfo.setId(projectInfoVo.getId());
        projectInfo.setFlowId(flowBizId);
        projectInfo.setProjectStatus(ProjectStatus.DELAY_CHECK.getCode());
        return sqlManager.updateTemplateById(projectInfo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int stopProjectInfo(ProjectInfoVo projectInfoVo) {
        ProjectInfo res = sqlManager.single(ProjectInfo.class,projectInfoVo.getId());
        //开启流程
        Long flowBizId = flowService.startFlow(res.getId(), "tb_project_info","project_status",
                res.getProjectStatus(),ProjectStatus.STOP.getCode(), FlowFlagEnum.PROJECT_DELAY.getCode());
        //记录日志
        ProjectLog log = new ProjectLog();
        log.setProjectId(res.getId());
        log.setFlowId(flowBizId);
        //中止研究理由
        log.setContentText(projectInfoVo.getContentText());
        log.setOperateType(OperateType.STOP_CHECK.getCode());
        AdminUserVO adminUserVO = AdminUserContext.getUser();
        log.setCreateBy(adminUserVO.getId());
        log.setCreateTime(new Date());
        projectLogService.insertTemplate(log);
        //更新项目
        ProjectInfo projectInfo = new ProjectInfo();
        projectInfo.setId(projectInfoVo.getId());
        projectInfo.setFlowId(flowBizId);
        projectInfo.setProjectStatus(ProjectStatus.STOP_CHECK.getCode());
        return sqlManager.updateTemplateById(projectInfo);
    }

    @Override
    public int finishProjectInfo(ProjectInfoVo projectInfoVo) {
        ProjectInfo res = sqlManager.single(ProjectInfo.class,projectInfoVo.getId());
        //开启流程
        Long flowBizId = flowService.startFlow(res.getId(), "tb_project_info","project_status",
                res.getProjectStatus(),ProjectStatus.FINISH.getCode(), FlowFlagEnum.PROJECT_CONCLUSION.getCode());
        //记录日志
        ProjectLog log = new ProjectLog();
        log.setProjectId(res.getId());
        log.setFlowId(flowBizId);
        //计划结题时间
        log.setPlanTime(projectInfoVo.getPlanTime());
        //实际结题时间
        log.setActualTime(projectInfoVo.getActualTime());
        //主要研究成果及其应用
        log.setContentText(projectInfoVo.getContentText());
        log.setOperateType(OperateType.FINISH_CHECK.getCode());
        AdminUserVO adminUserVO = AdminUserContext.getUser();
        log.setCreateBy(adminUserVO.getId());
        log.setCreateTime(new Date());
        projectLogService.insertTemplate(log);
        //更新项目
        ProjectInfo projectInfo = new ProjectInfo();
        projectInfo.setId(projectInfoVo.getId());
        projectInfo.setFlowId(flowBizId);
        projectInfo.setProjectStatus(ProjectStatus.FINISH_CHECK.getCode());
        return sqlManager.updateTemplateById(projectInfo);
    }
}
